package com.chuangshixinchen.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuangshixinchen.enterprise.common.Result;
import com.chuangshixinchen.enterprise.common.StatusCode;
import com.chuangshixinchen.enterprise.entity.Tb_enterprise;
import com.chuangshixinchen.enterprise.mapper.Tb_enterpriseMapper;
import com.chuangshixinchen.enterprise.service.ITb_enterpriseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangshixinchen.enterprise.vo.req.EnterpriseReqPageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-08-16
 */
@Service
public class Tb_enterpriseServiceImpl extends ServiceImpl<Tb_enterpriseMapper, Tb_enterprise> implements ITb_enterpriseService {

    @Override
    public Result<Tb_enterprise> getInfo(Long id) {
        Tb_enterprise enterprise = baseMapper.selectById(id);
        if (enterprise==null){
            return new Result<Tb_enterprise>(false, StatusCode.ERROR,"无此企业信息");
        }
        return new Result<Tb_enterprise>(true,StatusCode.OK,"查询成功",enterprise);
    }

    @Override
    public Result<Page<Tb_enterprise>> listPage(EnterpriseReqPageVo vo) {
        Page<Tb_enterprise> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        LambdaQueryWrapper<Tb_enterprise> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(vo.getUnitNumber())){
            wrapper.eq(Tb_enterprise::getUnitNumber, vo.getUnitNumber());
        }
        if (StringUtils.hasLength(vo.getUnitName())){
            wrapper.like(Tb_enterprise::getUnitName,vo.getUnitName());
        }
        Page<Tb_enterprise> userPage = baseMapper.selectPage(page, wrapper);
        return new Result<>(true, StatusCode.OK,"查询成功",userPage);
    }

    @Override
    public Result saveInfo(Tb_enterprise enterprise) {
        int changeNum = baseMapper.insert(enterprise);
        if (changeNum==0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @Override
    public Result updateInfo(Tb_enterprise enterprise) {
        int changeNum = baseMapper.updateById(enterprise);
        if (changeNum==0){
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }
}
